# -*- coding: UTF-8 -*-

# 在Python3.0测试通过
# 需要在gitlab里面新建一个AccessToken填入gitlabToken

import sys

if sys.version_info < (3, 0):
    import urllib
else:
    from urllib.request import urlopen

import json
import subprocess, shlex
import time
import os

# gitlabAddr GitLab域名  git.xxx.com
gitlabAddr = 'xxxxxx'
# gitlabToken GitLab访问token 登录-设置-访问token设置
gitlabToken = 'xxxxxx'

# sync syn projects
for index in range(10):
    url = "https://%s/api/v4/projects?private_token=%s&per_page=100&page=%d&order_by=name" % ( gitlabAddr, gitlabToken, index)
    print(url)

    if sys.version_info < (3, 0):
        allProjects = urllib.urlopen(url)
    else:
        allProjects = urlopen(url)

    allProjectsDict = json.loads(allProjects.read().decode(encoding='UTF-8'))
    if len(allProjectsDict) == 0:
        break
    for thisProject in allProjectsDict:
        try:
            thisProjectId = thisProject['id']
            thisProjectURL = thisProject['http_url_to_repo']
            thisProjectPath = thisProject['path_with_namespace']
            isContinue = True
            if not thisProjectPath.startswith('jiaoguan/yingji/'):
                continue

            print(thisProjectURL + ' ' + thisProjectPath)
            if not os.path.isdir(thisProjectPath):
                print("create project dir %s" % (thisProjectPath))
                os.makedirs(thisProjectPath)
                time.sleep(0.5)

            # syn branches
            branchesUrl = "https://%s/api/v4/projects/%s/repository/branches?private_token=%s" % (gitlabAddr,  thisProjectId , gitlabToken)
            print(branchesUrl)

            allBranches = urlopen(branchesUrl)
            allBranchesDict = json.loads(allBranches.read().decode(encoding='UTF-8'))
            for thisBranch in allBranchesDict:
                thisBranchName = thisBranch['name']
                thisBranchPath = thisProjectPath + '/' + thisBranchName
                if not os.path.exists(thisBranchPath):
                    print("create branch dir %s" % (thisBranchPath))
                    os.makedirs(thisBranchPath)
                    time.sleep(0.5)

                # sync code
                gitDirPath = thisBranchPath + '/.git'
                if os.path.exists(gitDirPath):
                    command = shlex.split('C:/Program Files/Git/bin/git -C "%s" pull' % (thisBranchPath))
                else:
                    command = shlex.split(
                        'C:/Program Files/Git/bin/git clone -b %s %s %s' % (thisBranchName,thisProjectURL, thisBranchPath))

                resultCode = subprocess.Popen(command)
                time.sleep(1)
        except Exception as e:
            print("Error on %s: %s" % (thisProjectURL, e.strerror))